package com.pany.rememberpass;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.pany.rememberpass.bean.WearBean;

public class InternetViewActivity extends AppCompatActivity {
    private TextView tv_Tem, tv_city, tv_result;
    private EditText et_city;
    private String city = "北京";
    private Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            Log.d("TAG", "handleMessage: msg.what  ====" + msg.what);
            if (msg.what == 1) {
                String obj = (String) msg.obj;
                tv_result.setText(obj);

                Gson gson = new Gson();
                WearBean wearBean = gson.fromJson(obj, WearBean.class);
                String city = wearBean.getCity();
                String wea = wearBean.getWea();

                tv_city.setText("city:" + city);
                tv_result.setText("result:" + wea);
                tv_Tem.setText("气温:" + wearBean.getTem_day() + "度");

                Toast.makeText(InternetViewActivity.this, "数据请求完成!!!", Toast.LENGTH_SHORT).show();
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_internet_view);
        tv_result = findViewById(R.id.tv_result);
        et_city = findViewById(R.id.et_city);
        tv_Tem = findViewById(R.id.tv_Tem);
        tv_city = findViewById(R.id.tv_city);

        et_city.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int i = 1;
                city = editable.toString().trim();
            }
        });

    }


    public void onClickResult(View view) {

        // 开启一起线程 做耗时操作 网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {

                Message message = new Message();

                String weatherOfCity = NetUtils2.getWeatherOfCity(city);

//                String doGet = NetUtils.doGet();
                message.what = 1;// 区别信息来源
                message.obj = weatherOfCity;// 这个是数据传输
                mHandler.sendMessage(message);// 发送到 主线程的 message queue
            }
        }).start();

        Toast.makeText(InternetViewActivity.this, "开始请求!!!", Toast.LENGTH_SHORT).show();
    }

    /*{"nums":0,"cityid":"101180101","city":"郑州",
    "date":"2022-05-14","week":"星期六","update_time":"15:30","
    wea":"多云","wea_img":"yun","tem":"20","tem_day":"21",
    "tem_night":"11","win":"西南风","win_speed":"2级","win_meter":"9km\/h",
    "air":"43","pressure":"1004","humidity":"39%"}
    **/


}